package com.tgy;

import java.util.Queue;
import java.util.Stack;

/**
 * @Author: tgy
 * @Date: 2020-09-04 09:52
 *
 * 使用两个stack实现队列
 */
public class TwoStackToQueue<E> {

    private Stack<E> inStack;

    private Stack<E> outStack;


    public TwoStackToQueue() {

        inStack = new Stack<>();

        outStack = new Stack<>();
    }

    /**
     * 添加元素
     * @param e
     * @return
     */
    boolean offer(E e) {

         inStack.push(e);

         return true;
    }

    /**
     * 判断队列是否为空
     * @return
     */
    boolean isEmpty() {


        return inStack.isEmpty() && outStack.isEmpty();
    }


    /**
     * 获取对头元素，并删除
     * @return the head of this queue, or {@code null} if this queue is empty
     */
    E poll() {


        if (outStack.isEmpty()) {

            while (!inStack.isEmpty()) {

                outStack.push(inStack.pop());
            }

        }

        return outStack.pop();
    }


    /**
     * 查看对头元素
     * @return the head of this queue, or {@code null} if this queue is empty
     */
    E peek() {

        return outStack.peek();
    }
}
